package com.ll.daipao.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.ll.daipao.handler.Constants;
import com.ll.daipao.wechat.service.OrderService;
import com.ll.daipao.wechat.vo.TOrder;
import com.ll.daipao.wechat.vo.TOrderType;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by rockyll on 2017/10/26.
 */
@Controller
@RequestMapping("send")
public class SendOrderController extends BaseController
{
    @Autowired
    OrderService orderService;

    @RequestMapping("taskObtain")
    public String taskObtain (HttpServletRequest request){
        List<TOrderType> tOrderTypes = orderService.selectOrderType();
        request.setAttribute("ordertypes" ,tOrderTypes );
        return "taskObtain";
    }

    /**
     * 发单
     * @param request
     * @return
     */
    @RequestMapping("saveOrder")
    @ResponseBody
    public String saveOrder(HttpServletRequest request ){

        String openId = (String) request.getSession().getAttribute(Constants.WX_OPENID);

        String order_title = request.getParameter("order_title");
        String order_desc = request.getParameter("order_desc");
        String re_address = request.getParameter("re_address");
        String tel = request.getParameter("tel");
        String amount = request.getParameter("amount");
        String order_type = request.getParameter("order_type");
        String btime = request.getParameter("btime");
        String etime = request.getParameter("etime");
        if(StringUtils.isBlank(order_title)
                ||StringUtils.isBlank(order_desc)
                ||StringUtils.isBlank(re_address)
                ||StringUtils.isBlank(tel)
                ||StringUtils.isBlank(amount)
                ||StringUtils.isBlank(order_type)
                ||StringUtils.isBlank(btime)
                ||StringUtils.isBlank(etime)){
            return  failed("关键信息不得为空！");
        }
        TOrder tOrder = new TOrder();
        tOrder.setAmount(Long.parseLong(amount));
        tOrder.setOrderDesc(order_desc);
        tOrder.setOrderTitle(order_title);
        tOrder.setReAddress(re_address);
        tOrder.setTel(tel);
        tOrder.setOrderType(order_type);
        tOrder.setReBeginTime(new Date());
        tOrder.setReEndTime(new Date());
        JSONObject res = null;
        try {
            res = orderService.saveOrder(openId, tOrder);
        } catch (Exception e) {
            this.logger.error(e.getMessage(),e);
            return failed();
        }
        if(res.getBoolean("suc")){
            res.remove("suc");
            return success(res);
        }
        return failed(res.getString("msg"));
    }

}
